package net.cl.support.codec;

import java.util.zip.Checksum;

/**
 * Cyclic Redundancy Check (Code) 循环冗余校验(码)
 * 
 * @author caol64@gmail.com
 */
public class CRC32 {

	/**
	 * CRC32校验(8位)
	 */
	public static String encode(byte[] in) throws CodecException {
		if (in == null)
			return null;
		try {
			Checksum crc32 = new java.util.zip.CRC32();
			crc32.update(in, 0, in.length);
			long checksum = crc32.getValue();
			crc32.reset();
			return Long.toHexString(checksum);
		} catch (Exception e) {
			throw new CodecException(e);
		}
	}
	
	/**
	 * CRC32校验(8位)
	 */
	public static String encode(String in) throws CodecException {
		if (in == null)
			return null;
		return encode(in.getBytes());
	}
	
}
